//
//  MeatPriceViewController.m
//  PigMarket
//
//  Created by 张敬 on 2019/7/30.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatPriceViewController.h"
#import "MeatPriceTableViewCell.h"
#import "MeatSearchHeader.h"
#import "TJBannerView.h"
#import "MeatMarketApi.h"


@interface MeatPriceViewController ()<XXTGBannerDelegate>

@property (nonatomic, strong) MeatMarketApi *meatApi;
@property (nonatomic, strong) BuyMeatadvertiseListModel *bannerModel;
@property (nonatomic, strong) MeatPricePage *priceModel;

@property (nonatomic, strong) MeatSearchHeader *navView;
@property (nonatomic, strong) TJBannerView *bannerView;

@property (nonatomic, strong) UILabel *rightNavLab;
@property (nonatomic, strong) UIView *tipView;
@property (nonatomic, strong) NSMutableArray *datas;

@end

@implementation MeatPriceViewController

#pragma mark - lifecycle methods

- (void)setupUI{

    {
        self.baseNavView.hidden = YES;
        [self.view addSubview:self.navView];
        
        @weakify(self);
        _navView.leftNavBlock = ^{
            @strongify(self);
            [self.navigationController popViewControllerAnimated:YES];
        };

    }
    
    {
        [self.view addSubview:self.tipView];
        [self.view addSubview:self.bannerView];
    }
    
}

- (void)setupLayout{
    
    {
        _navView.navView.width = KScreenWidth - 140;
        _navView.searchTextField.width = _navView.width - 34;
    }
    
    {
        _tipView.mj_y = TJ_NavBarHeight;
        _bannerView.mj_y = _tipView.bottom;
    }
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.bottom.left.mas_equalTo(0);
        make.top.mas_equalTo(self.bannerView.bottom);
    }];
}

- (void)initData{
    
    [self requestBuyMeatAdvertiseData];
    NSNumber *page = [NSNumber numberWithInteger:self.page];
    [self requestPriceDataByPage:page];
}

#pragma mark - networking methods

//肉市-卖肉-广告
- (void)requestBuyMeatAdvertiseData {
    
    @weakify(self);
    [[self.meatApi getBuyMeatAdvertiseListData] subscribeNext:^(BuyMeatadvertiseListModel * _Nullable bannerModel) {
        @strongify(self);
        
        NSMutableArray *aims = [NSMutableArray array];
        [bannerModel.list enumerateObjectsUsingBlock:^(BuyMeatadvertiseModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [aims addObject:obj.imgName];
        }];
        self.bannerView.imageArray = [aims copy];
        
    }];
}

- (void)requestPriceDataByPage:(NSNumber *)page{
    
    @weakify(self);
    [[self.meatApi getMeatPriceReferenceByTribeId:@1 page:page] subscribeNext:^(MeatPriceModel * _Nullable model) {
        @strongify(self);
        
        if (self.isRefresh) {
            
            [self endRefresh];
            if (self.datas.count >0) {
                [self.datas removeAllObjects];
            }
        }
        
        self.priceModel = model.page;
        [self.priceModel.list enumerateObjectsUsingBlock:^(MeatPriceItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [self.datas addObject:obj];
        }];

        if ([self.priceModel.currPage intValue] >= [self.priceModel.totalPage intValue]) {
            [self endLoadMoreMoData];
        }else{
            [self endLoadMore];
        }
        
        if (self.datas.count >0) {
            self.tableView.hidden = NO;
            [self.tableView reloadData];
        }else{
            [self loadNoData];
        }
        
    }error:^(NSError * _Nullable error) {
        @strongify(self);
        [self loadErrorData];
    }];
    
}


//下拉刷新
- (void)refresh
{
    [super refresh];
    [self resetData];
    [self initData];
}

//上拉加载更多
- (void)loadMore
{
    [super loadMore];
    NSNumber *page = [NSNumber numberWithInteger:self.page];
    [self requestPriceDataByPage:page];
}


#pragma mark - TJBannerView.delegate methods

- (void)banber:(TJBannerView *)bannerView clickedBannerIndex:(NSInteger)index
{
}


#pragma mark - tableview delegate/dataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.priceModel.list.count + 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return indexPath.row == 0 ?44 :54;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MeatPriceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeatPriceTableViewCell"];
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }

    if (indexPath.row == 0) {
        cell.type = MeatPriceTableViewCellType_Blod;
        cell.typeNameLab.text = @"部位";
        cell.blackPriceLab.text = @"黑猪参考单价";
        cell.whitePriceLab.text = @"白猪参考单价";
        return cell;
    }
    
    cell.type = MeatPriceTableViewCellType_Default;
    
    if (indexPath.row < self.priceModel.list.count +1) {
        
        MeatPriceItem *item = self.priceModel.list[indexPath.row -1];
        
        cell.typeNameLab.text = item.positionName;
        cell.blackPriceLab.text = [NSString stringWithFormat:@"¥ %@", item.wUnitPrices];
        cell.whitePriceLab.text = [NSString stringWithFormat:@"¥ %@", item.bUnitPrices];
    }
    
    return cell;
}


#pragma mark - getter methods

- (MeatSearchHeader *)navView{
    
    if (!_navView) {
        _navView = [[MeatSearchHeader alloc] init];
        [_navView addSubview:self.rightNavLab];
    }
    return _navView;
}


- (UILabel *)rightNavLab{
    
    if (!_rightNavLab) {
        _rightNavLab = [UILabel new];
        _rightNavLab.size = CGSizeMake(80, 17);
        _rightNavLab.mj_y = TJ_StatusBarHeight_Define + 14;
        _rightNavLab.mj_x = KScreenWidth - 80;
        
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"更改部落"];
        [attributedString addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0x666666) range:NSMakeRange(0, 4)];//字体颜色
        [attributedString addAttribute:NSFontAttributeName value:mFont(12) range:NSMakeRange(0, 4)];//字体大小
        CGSize size = [UIImage imageNamed:@"adopt_down"].size;
        
        NSTextAttachment *attchment1 = [[NSTextAttachment alloc]init];
        attchment1.bounds = CGRectMake(2, 0, size.width, size.height);//设置frame
        attchment1.image = [UIImage imageNamed:@"adopt_down"];//设置图片
        NSAttributedString *string1 = [NSAttributedString attributedStringWithAttachment:(NSTextAttachment *)(attchment1)];
        [attributedString appendAttributedString:string1];
        
        _rightNavLab.attributedText = attributedString;
        
        @weakify(self);
        _rightNavLab.tapBlock = ^{
            @strongify(self);
            [self showHudWithMsg:@"更改部落"];
        };
    }
    return _rightNavLab;
}

- (UIView *)tipView{
    
    if (!_tipView) {
        
        _tipView = [UIView makeViewBgColor:KWhiteColor cornerRadius:0];
        _tipView.frame = CGRectMake(0, 0, KScreenWidth, 52);
        
        UIImageView *icon1 = [UIImageView makeImageByImageName:@"meat_buy_start" cornerRadius:0];
        icon1.frame = CGRectMake(15, 15, 20, 20);
        [_tipView addSubview:icon1];
        
        UIImageView *icon2 = [UIImageView makeImageByImageName:@"meat_price_bg" cornerRadius:0];
        icon2.frame = CGRectMake(icon1.right + 5 , 18, 128, 16);
        [_tipView addSubview:icon2];
        
        UILabel *lab = [UILabel makeLabelWithText:@"元/500g" textColor:UIColorFromRGB(0x666666) font:mFont(12) textAliment:0];
        lab.frame = CGRectMake(icon2.right + 5, 18, 100, 16);
        [_tipView addSubview:lab];
    }
    return _tipView;
}


- (TJBannerView *)bannerView
{
    if (!_bannerView) {
        _bannerView = [[TJBannerView alloc] init];
        _bannerView.frame = CGRectMake(0, 0, KScreenWidth , 140);
        _bannerView.delegate = self;
    }
    return _bannerView;
}

- (MeatMarketApi *)meatApi{
    
    if(!_meatApi){
        _meatApi = [[MeatMarketApi alloc]init];
    }
    return _meatApi;
}

- (MeatPricePage *)priceModel{
    
    if (!_priceModel) {
        _priceModel = [[MeatPricePage alloc] init];
    }
    return _priceModel;
}

- (NSMutableArray *)datas{
    
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}

#pragma mark - override methods

- (void)registerTableViewCell{
    
    self.tableView.separatorColor = mIconLineColor;
    [self.tableView registerClass:[MeatPriceTableViewCell class] forCellReuseIdentifier:@"MeatPriceTableViewCell"];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 10)];
    view.backgroundColor = mBackgroudColor;
    self.tableView.tableFooterView = view;
}

- (void)loadErrorData
{
    [super loadErrorData];
    self.tipView.hidden = YES;
    self.bannerView.hidden = YES;
}

- (void)loadNoData
{
    [super loadNoData];
    self.tipView.hidden = YES;
    self.bannerView.hidden = YES;
}


- (void)endLoadMore{
    [super endLoadMore];
    self.tipView.hidden = NO;
    self.bannerView.hidden = NO;
    
}

- (void)endRefresh{
    [super endRefresh];
    self.tipView.hidden = NO;
    self.baseNavView.hidden = NO;
    
}

- (void)endLoadMoreMoData
{
    [super endLoadMoreMoData];
    self.tipView.hidden = NO;
    self.baseNavView.hidden = NO;
}


@end
